package com.lagou.demo.service.impl;

import com.lagou.demo.dao.AccountDao;
import com.lagou.demo.pojo.Account;
import com.lagou.demo.service.SecurityService;
import com.lagou.demo.utils.ConnectionUtils;
import com.lagou.edu.mvcframework.annotations.LagouAutowired;
import com.lagou.edu.mvcframework.annotations.LagouService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@LagouService("securityService")
public class SecurityServiceImpl implements SecurityService {
    @LagouAutowired
    private AccountDao accountDao;

    @LagouAutowired
    private ConnectionUtils connectionUtils;

    @Override
    public boolean queryByName(String name) throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //从连接池获取连接
            Connection con = connectionUtils.getCurrentThreadConn();
            String sql = "select * from account where name=?";
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, name);
            resultSet = preparedStatement.executeQuery();

//            Account account = new Account();
            while (resultSet.next()) {
                return true;
            }
            return false;
        } finally {
            resultSet.close();
            preparedStatement.close();
        }
    }
}
